import os

#python -u "D:\GitbookLibrary\MarkdownTableMultifile.py"

#新建列表,每三個輸入一個\\hline
print("請輸入列數")
column = int(input())
#ColumnStyle = "|c" * column + "|"

def getCellsbyInput():
    print("請輸入表格內容")
    cells = []
    while True:
        cell = input()
        if cell != "jieshu":
            cells.append(cell)
            continue
        else:
            break
    return cells
def getCellsbyFile(filepath,code):
    InputCells = []
    f1 = open(filepath,"r",encoding = code)
    InputLinecs = f1.readlines()
    f1.close()
    for line in InputLinecs:
        LineCells = line.strip("\n").split(";;")
        CellNum = 0
        for LineCell in LineCells:
            if LineCell.count("$") == 1:
                print(LineCell+"$ error\n")
            InputCells.append(LineCell)
            CellNum += 1
        if CellNum != 3:
            print("CellNum error")
    return InputCells

path = "D:\\GitbookLibrary\\"
OutputFile = "Discrete-Mathematics-symbol"
TableHead = "| a    | b    | c    |"
f = open(path+OutputFile+".md","w",encoding="utf-8")

#cells = getCellsbyInput()
#cells = getCellsbyFile(path+OutputFile+".txt","utf-8")
cells = []
files = os.listdir(path+"Discrete-Mathematics-symbol-table\\")
for file in files:
    if "table" in file:
        print(file)
        cells.append(getCellsbyFile(path+"Discrete-Mathematics-symbol-table\\"+file,"utf-8"))
'''f.write("\\documentclass{article}\n\\usepackage{ctex}\n\\usepackage{supertabular}\n\\usepackage{latexsym}\n\\usepackage{amssymb}\n")
#f.write("\\usepackage[landscape]{geometry}\n")#橫置頁面
f.write("\\begin{document}\n")'''
num = 0
while num < len(files):
    #print(files[num][7:len(files[num])-4])
    #f.write("\n\n~\\\\\n~\\\\\n"+files[num][7:len(files[num])-4]+"\\\\\n\\begin{supertabular}{"+ColumnStyle+"}\n\\hline\n")
    f.write("\n\n# "+files[num][7:len(files[num])-4]+"\n"+TableHead+"\n| ---- | ---- | ---- |\n|")
    num2 = 0
    while num2 < len(cells[num]):
        if (num2+1) % column == 0:
            f.write(cells[num][num2]+"|\n|")
            num2 += 1
            continue
        else:
            f.write(cells[num][num2]+"|")
            num2 += 1
            continue
    #f.write("\\end{supertabular}\n")
    num += 1
f.close()
f = open(path+OutputFile+".md","r",encoding="utf-8")
lines = f.readlines()
f.close()
f = open(path+OutputFile+".md","w",encoding="utf-8")
num = 0
while num < len(lines):
    if lines[num] == "|\n" or lines[num] == "\n":
        num += 1
        continue
    else:
        f.write(lines[num])
        num += 1
        continue
f.close()
f = open(path+OutputFile+".md","r",encoding="utf-8")
lines = f.readlines()
num = 0
while num < len(lines):
    print(str(num+1).zfill(3)+" "+lines[num],end = "")
    num += 1
    continue
f.close()